<template>
  <el-col>
    <el-dialog title="增加" :visible.sync="addDialogVisible" :before-close="handleCloseAddDialog">
      <el-form ref="hospitalInfo" :model="hospitalInfo" :rules="rules" label-width="100px">
        <el-form-item label="医院名字" prop="name">
          <el-input v-model="hospitalInfo.name" />
        </el-form-item>
        <el-form-item label="医院类型" prop="hospitalType">
          <el-select v-model="hospitalInfo.hospitalType" placeholder="医院类型">
            <el-option v-for="(item, index) in hospitalTypeList" :key="index" :value="item.name" :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="hospitalInfo.address" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="hospitalInfo.status" type="text" placeholder="状态">
            <el-option :value="0" label="在线"> </el-option>
            <el-option :value="1" label="下线"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseAddDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAddDialog">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="更新" :visible.sync="updateDialogVisible" :before-close="handleCloseUpdateDialog">
      <el-form ref="hospitalInfo" :model="hospitalInfo" :rules="rules" label-width="100px">
        <el-form-item label="医院名字" prop="name">
          <el-input v-model="hospitalInfo.name" />
        </el-form-item>
        <el-form-item label="医院类型" prop="hospitalType">
          <el-select v-model="hospitalInfo.hospitalType" placeholder="医院类型">
            <el-option v-for="(item, index) in hospitalTypeList" :key="index" :value="item.name" :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="hospitalInfo.address" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="hospitalInfo.status" type="text" placeholder="状态">
            <el-option :value="0" label="在线"> </el-option>
            <el-option :value="1" label="下线"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseUpdateDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdateDialog">确 定</el-button>
      </span>
    </el-dialog>

    <el-row>
      <el-col :span="2">
        <el-button type="primary" @click="addDialogVisible = true">增加</el-button>
      </el-col>

      <el-col :span="2">
        <el-button :disabled="multipleSelection.length > 1" type="warning"
          @click="showUpdateDialogVisible">更新</el-button>
      </el-col>

      <el-col :span="2">
        <el-button type="danger" @click="handleDeleteSelectIds">删除</el-button>
      </el-col>

      <el-col :span="5">
        <el-input v-model="query.name" type="text" placeholder="医院名" />
      </el-col>
      <el-col :span="5">
        <el-select v-model="query.hospitalType" placeholder="医院类型">
          <el-option v-for="(item, index) in hospitalTypeList" :key="index" :value="item.name" :label="item.name">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <el-input v-model="query.address" type="text" placeholder="地址" />
      </el-col>
      <el-col :span="5">
        <el-select v-model="query.status" type="text" placeholder="状态">
          <el-option :value="0" label="在线"> </el-option>
          <el-option :value="1" label="下线"> </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button @click="queryClear"> 清除 </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="doSelectAll"> 搜索 </el-button>
      </el-col>
    </el-row>

    <el-table ref="multipleTable" :data="hospitalList" style="width: 100%" row-key="hospitalId" lazy
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="hospitalId" label="医院 ID" />
      <el-table-column prop="name" label="医院名" />
      <el-table-column prop="hospitalType" label="医院类型" />
      <el-table-column prop="address" label="地址" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 0">在线</el-tag>
          <el-tag type="danger" v-if="scope.row.status === 1">下线</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" :total="total" @prev-click="currentChange"
      @next-click="currentChange" @current-change="currentChange" />
  </el-col>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      addDialogVisible: false,
      updateDialogVisible: false,

      hospitalList: [],
      hospitalTypeList: [],

      page: 0,
      pageSize: 10,
      total: 0,

      multipleSelection: [],

      hospitalInfo: {
        hospitalId: "",
        name: "",
        hospitalType: "",
        address: "",
        status: "",
      },

      rules: {
        name: [{ required: true, trigger: "blur" }],
        hospitalType: [{ required: true, trigger: "blur" }],
        address: [{ required: true, trigger: "blur" }],
        status: [{ required: true, trigger: "blur" }],
      },

      query: {
        name: "",
        hospitalType: "",
        address: "",
        status: "",
      },
    };
  },
  methods: {
    currentChange(index) {
      this.page = index - 1;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    queryClear() {
      this.query = {
        name: "",
        hospitalType: "",
        address: "",
        status: "",
      };
      this.doSelectAll();
    },

    doSelectAll() {
      this.$store
        .dispatch("hospital/selectAll", {
          obj: {
            name: this.query.name,
            hospitalType: this.query.hospitalType,
            address: this.query.address,
            status: this.status != "" ? this.status : 0,
          },
          page: this.page,
          pageSize: this.pageSize,
        })
        .then((res) => {
          this.hospitalList = res.list;
          this.total = res.total;
        });
    },
    doSelectHospitalTypeId() {
      this.$store.dispatch("hospitalType/allId", {}).then(res => {
        this.hospitalTypeList = res
      })
    },

    handleConfirmAddDialog() {
      this.$store.dispatch("hospital/add", this.hospitalInfo);
      this.addDialogVisible = false;
      this.refresh();
    },
    handleCloseAddDialog() {
      this.addDialogVisible = false;
      this.refresh();
    },

    showUpdateDialogVisible() {
      if (this.multipleSelection.length === 0) {
        this.$message.error("请选择");
        return;
      }
      this.hospitalInfo = this.multipleSelection[0];
      this.updateDialogVisible = true;
    },
    handleConfirmUpdateDialog() {
      this.$store.dispatch("hospital/update", this.hospitalInfo);
      this.updateDialogVisible = false;
      this.refresh();
    },
    handleCloseUpdateDialog() {
      this.updateDialogVisible = false;
      this.refresh();
    },

    handleDeleteSelectIds() {
      const ids = [];
      for (const obj of this.multipleSelection) {
        ids.push(obj.hospitalId);
      }
      this.$store.dispatch("hospital/delete", ids).then(() => { });
      this.doSelectAll();
    },

    refresh() {
      this.hospitalInfo = {
        name: "",
        hospitalType: "",
        address: "",
        status: "",
      };
      this.doSelectAll()
      this.$refs.multipleTable.clearSelection();
    },
  },
  created() {
    this.doSelectAll();
    this.doSelectHospitalTypeId()
  },
};
</script>

<style scoped>

</style>